package com.example.controller;

import com.graduation.dto.LateralMovementDO;
import com.graduation.mapper.AttackTaskAssignMapper;
import com.graduation.mapper.LateralMovementMapper;
import com.graduation.pojo.*;
import com.graduation.vo.EmployeeVO;
import com.graduation.vo.LateralMovementVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.stream.Collectors;

@Controller
@WebException
@RequestMapping("/lateral-movement")
public class LateralMovementController extends BaseController<LateralMovementDO, LateralMovementVO> {

    @Resource
    private LateralMovementMapper lateralMovementMapper;
    @Resource
    private AttackTaskAssignMapper taskAssignMapper;

    @ResponseBody
    @GetMapping("/get")
        public WebResult<LateralMovementVO> get(Integer id) {
        return new WebResult<>(getVoByDo(lateralMovementMapper.selectById(id), LateralMovementVO.class));
    }

    @ResponseBody
    @PostMapping("/page")
    public WebResult<PageResult<LateralMovementVO>> page(HttpSession session, LateralMovementQuery query) {
        EmployeeVO loginer = (EmployeeVO) session.getAttribute("loginer");
        query.setTesterEq(loginer.getName());
        PageResult<LateralMovementVO> result = new PageResult<>();
        result.setRowCount(lateralMovementMapper.selectCount(query));
        result.setPageCount((result.getRowCount() - 1) / query.getPageSize() + 1);
        if (query.getPageNum() > result.getPageCount()) {
            query.setPageNum(result.getPageCount());
        }
        result.setPageNum(query.getPageNum());
        // 设置分页查询偏移量
        query.setLimit((query.getPageNum() - 1) * query.getPageSize());
        result.setEntityList(lateralMovementMapper.selectList(query).stream()
                .map(e -> getVoByDo(e, LateralMovementVO.class))
                .collect(Collectors.toList()));
        return new WebResult<>(result);
    }

    @ResponseBody
    @PostMapping("/operate")
    public WebResult<?> operate(HttpSession session, LateralMovementRequest request) {
        LateralMovementDO lateralMovementDO = getDoByRequest(request, LateralMovementDO.class);
        if (Objects.isNull(lateralMovementDO.getId())) {
            lateralMovementDO.setCreateTime(LocalDateTime.now());
            lateralMovementDO.setUpdateTime(LocalDateTime.now());
            EmployeeVO loginer = (EmployeeVO) session.getAttribute("loginer");
            lateralMovementDO.setTester(loginer.getName());
            lateralMovementMapper.insert(lateralMovementDO);
        } else {
            lateralMovementDO.setUpdateTime(LocalDateTime.now());
            lateralMovementMapper.updateById(lateralMovementDO);
        }
        return WebResult.success();
    }

    @ResponseBody
    @DeleteMapping("/delete")
    public WebResult<?> delete(Integer id) {
        lateralMovementMapper.deleteById(id);
        return WebResult.success();
    }

    @RequestMapping("/to-lateral-movement")
    public String toLateralMovement() {
        return "attack/lateralMovementAdmin";
    }

    @RequestMapping("/to-operate")
    public String toOperate(Model model, HttpSession session, Integer id) {
        model.addAttribute("id", id);
        model.addAttribute("movement", Objects.isNull(id) ? new LateralMovementDO(): lateralMovementMapper.selectById(id));
        EmployeeVO loginer = (EmployeeVO) session.getAttribute("loginer");
        AttackTaskAssignQuery assignQuery = new AttackTaskAssignQuery();
        assignQuery.setExecutorEq(loginer.getName());
        model.addAttribute("taskList", taskAssignMapper.selectList(assignQuery));
        return "attack/operateLateralMovement";
    }
}
